package com.temi.iot.common.constant.factory;

import com.github.pagehelper.PageHelper;
import com.temi.iot.common.constant.result.Page;
import com.temi.iot.kit.HttpKit;

import java.util.List;
import java.util.function.Supplier;

/**
 * Created by
 *
 * @author sheng
 * @date 18/3/16.
 */
public class PageFactory {

    public static <T> Page<T> page(Supplier<List<T>> supplier) {
        Integer pageNo;
        Integer pageSize;
        try {
            pageNo = Integer.valueOf(HttpKit.getRequestParameters().get("pageNo"));
            pageSize = Integer.valueOf(HttpKit.getRequestParameters().get("pageSize"));
        } catch (Exception e) {
            pageNo = 0;
            pageSize = 0;
        }
        com.github.pagehelper.Page page = PageHelper.startPage(pageNo, pageSize);
        List<T> data = supplier.get();
        return Page.create(page.getPageNum(), (int) page.getTotal(), page.getPageSize(), data);
    }

}
